import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

const args = process.argv.slice(2);
const targetPageParma = args.find(n => n.startsWith('target-page='));
if (!targetPageParma) {
  throw new Error('target-page not specified.');
}
const pageName = targetPageParma.split('=')[1];
if (!pageName) {
  throw new Error('value of target-page is empty.');
}
console.log(`pageName is 【${pageName}】`);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  build: {
    lib: {
      name: pageName,
      entry: `./src/pages/${pageName}/index.js`,
      fileName: (format)=> `${pageName}.${format}.js`
    },
    outDir: `dist/${pageName}/`,
    emptyOutDir: true,
    copyPublicDir: false,
  },
});
